#include<iostream>
using namespace std;
struct AVLTreeNode {
    AVLTreeNode(int t) :val(t), left(nullptr), right(nullptr), _parent(nullptr) {}
    int val;
    int bf = 0;
    AVLTreeNode* left;
    AVLTreeNode* right;
    AVLTreeNode* _parent;
};
struct AVLTree {
    typedef AVLTreeNode Node;
    void insert(int e) {
        if (_root == nullptr) {
            _root = new Node(e);
            return;
        }
        Node* cur = _root;
        Node* parent = nullptr;
        while (cur) {
            if (cur->val > e) {
                parent = cur;
                cur = cur->left;
            }
            else if (cur->val < e) {
                parent = cur;
                cur = cur->right;
            }
        }
        cur = new Node(e);
        if (parent->val > e){
            parent->left = cur;
            cur->_parent = parent;
        }
        else{
            parent->right = cur;
            cur->_parent = parent;
        }

        while (parent)
        {
            if (parent->left == cur) parent->bf--;
            else parent->bf++;

            if (parent->bf == 0) break;
            else if (parent->bf == 1 || parent->bf == -1) {
                cur = parent;
                parent = parent->_parent;
            }
            else if (parent->bf == 2 || parent->bf == -2) {
                if (parent->bf == 2) {
                    if (parent->right->bf == 1) {
                        RotateL(parent);
                    }
                    else {
                        RotateRL(parent);
                    }
                }
                else {
                    if (parent->left->bf == -1) {
                        RotateR(parent);
                    }
                    else {
                        RotateLR(parent);
                    }
                }
                break;
            }
        }

    }
    void RotateL(Node* parent)
    {
        Node* P = parent->_parent;
        Node* subR = parent->right;
        Node* subRL = subR->left;
        parent->right = subRL;
        if (subRL) subRL->_parent = parent;
        subR->left = parent;
        parent->_parent = subR;
        subR->_parent = P;
        if (P) {
            if (P->left == parent) P->left = subR;
            else P->right = subR;
        }
        else {
            _root = subR;
        }
        parent->bf = subR->bf = 0;
    }
    void RotateR(Node* parent)
    {
        Node* P = parent->_parent;
        Node* subL = parent->left;
        Node* subLR = subL->right;
        parent->left = subLR;
        if (subLR) subLR->_parent = parent;
        subL->right = parent;
        parent->_parent = subL;
        subL->_parent = P;
        if (P) {
            if (P->left == parent) P->left = subL;
            else P->right = subL;
        }
        else {
            _root = subL;
        }
        parent->bf = subL->bf = 0;
    }
    void RotateLR(Node* parent)
    {
        Node* subL = parent->left;
        Node* subLR = subL->right;
        int bf = subLR->bf;
        RotateL(subL);
        RotateR(parent);
        if (bf == 0) {
            subL->bf = subLR -> bf = parent->bf = 0;
        }
        else if (bf == 1) {
            subL->bf = -1;
            subLR -> bf = parent->bf = 0;
        }
        else if (bf == -1) {
            parent->bf = 1;
            subL->bf = subLR -> bf = 0;
        }
    }
    void RotateRL(Node* parent)
    {
        Node* subR = parent->right;
        Node* subRL = subR->left;
        int bf = subRL->bf;
        RotateR(subR);
        RotateL(parent);
        if (bf == 0) {
            subR->bf = subRL -> bf = parent->bf = 0;
        }
        else if (bf == 1) {
            parent->bf = -1;
            subR->bf = subRL -> bf = 0;
        }
        else if (bf == -1) {
            subR->bf = 1;
            subRL -> bf = parent->bf = 0;
        }
    }
    Node* _root = nullptr;
};
int main()
{
    AVLTree tree;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        tree.insert(x);
    }
    cout << tree._root->val << endl;
    return 0;
}